
#include "display.hpp"

Forte::Display::Display()
  : mHeight(0)
  , mWidth(0)
  , mDividerY(0)
{
  pthread_mutex_init(&mMutex, NULL);
  initscr();            // start curses mode
  cbreak();             // disable line buffering
  noecho();             // don't echo() while getch()
  keypad(stdscr, TRUE); // enable F1, F2, arrows, etc.
  start_color();        // start color functionality
  init_pair(1, COLOR_WHITE, COLOR_BLUE);
  init_pair(2, COLOR_WHITE, COLOR_RED);
}

Forte::Display::~Display()
{
  pthread_mutex_destroy(&mMutex);
}

void Forte::Display::OnBuildBegin()
{
  mBuilding = true;
  mErrorLines.clear();
  mTextLines.clear();
}

void Forte::Display::OnBuildEnd()
{
  mBuilding = false;
}

void Forte::Display::OnError(SimpleString const& filename, unsigned linenum,
                             SimpleString const& text)
{
  char buffer[512];
  snprintf(buffer, sizeof(buffer), "%.*s:%u \033[31merror\033[0m: %.*s",
           filename.GetLength(), filename.GetData(), linenum, text.GetLength(), text.GetData());
  buffer[sizeof(buffer)-1] = 0;
  std::string s(buffer);
  pthread_mutex_lock(&mMutex);
  mErrorLines.push_back(s);
  pthread_mutex_unlock(&mMutex);
}
